<?php
defined('BASEPATH') or exit('No direct script access allowed');
require_once (dirname(__FILE__) . "/../../alipay/alipay.config.php");
require_once (dirname(__FILE__) . "/../../alipay/lib/alipay_notify.class.php");

class Alipay2 extends CI_Controller {
	var $alipay_config;
	public function __construct (){
		parent::__construct();
		$this->config->load('alipay', TRUE);
		$this->alipay_config = $this->config->item('alipay');
	}

	public function notify_url() {
		$alipayNotify = new AlipayNotify($this->alipay_config);
		$verify_result = $alipayNotify->verifyNotify();
		if($verify_result){
			$trade_status=$this->post->get('trade_status');
			if($trade_status == 'TRADE_FINISHED') {
				echo "success";
			}else if ($trade_status == 'TRADE_SUCCESS') {
				echo "success";
			}
		}else{
			echo "fail";
		}
	}
	
	public function call_back_url() {
		//var_dump($this->alipay_config);
		//die();
		
		//$this->load->library('alipay/alipay_notify', $this->alipay_config);
		//$verify_result = $this->alipay_notify->verifyReturn();
		$alipayNotify = new AlipayNotify($this->alipay_config);
		$verify_result = $alipayNotify->verifyReturn();
		//var_dump($verify_result);
		//die();
		
		if($verify_result) {//验证成功
			$orderno=$this->input->get('out_trade_no');
			$this->load->model ( 'order_model' );
			$order=$this->order_model->find_by_orderno($orderno);
			if($order['state']==5){//货到付款未结算
				$neworder= array (
				'state' => 4,
				'payment_time'=>date ( 'Y-m-d H:i:s', time () )
				);
			}else{
				$neworder= array (
				'state' => 1,
				'payment_mode'=> 1,
				'payment_time'=>date ( 'Y-m-d H:i:s', time () )
				);
			}
			if($this->order_model->update($order['id'],$neworder)){
				$this->load->model ( 'user_model' );
				$user = $this->user_model->load ( $order['userid'] );
				$num=$this->order_model->find_by_ordertime( $order['userid'], date ( "Y-m-d") );//当天下单次数
				if($num==1){
					$this->sendmessagetouser($order,$user);
				}
				$data['orderid']=$order['id'];
				$this -> load -> view('alipay/success',$data);
			}
		} else {
			echo "验证失败";
		}
		
	}
	
	/*
	 * 将订单信息通过短信发送给用户
	 */
	private function sendmessagetouser($order,$user){
		$date=date ( "n月j日",strtotime($order ['receive_time'] ));
		if(!empty($user['late_time'])){
			$date=$date.','.$user['late_time'];
		}else{
			$date=$date.','.'-';
		}
		$phone=$user['cellphone'];
		
		$sid="9f014d8f371d9a94348092594b971d3f";
		$appid="55953fa1a9014153b89e194bee63770a";
		$key="d4435599239290ed5e40df062cc85e37";
		$time=date("YmdHis").'111';
		$sign=md5($sid.$time.$key);
		$templateid="11021";
		
		$url="http://www.ucpaas.com/maap/sms/code?sid=".$sid."&appId=".$appid."&time=".$time."&sign=".$sign."&to=".$phone."&templateId=".$templateid."&param=".$date;
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		$output = curl_exec ( $ch );
		curl_close ( $ch );
		$output = json_decode($output);
		if($output->resp->respCode=="000000"){//短信发送成功
			$message = array (
					'phone' => $phone,
					'state' => 1,
					'smsid' => $output->resp->templateSMS->smsId,
					'createdate' => $output->resp->templateSMS->createDate
			);
			$this->load->model ( 'message_model' );
			$this->message_model->create($message);//更新短信表
		}
	}

}
